package com.byq.mm.smartlib.tools;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.byq.mm.smartlib.activity.SubDevicesActivity;
import com.byq.mm.abstractx.device.Device;
import com.byq.mm.smartlib.R;

public class SimpleDeviceAdapter extends BaseAdapter {
	private LayoutInflater inflator;
	Context context;
	boolean action;
	public SimpleDeviceAdapter(Context ctx, Device device, boolean action) {
		super();
		this.devices = device.getSubdevices().toArray(new Device[0]);
		this.context = ctx;
		inflator = LayoutInflater.from(ctx);
		this.action = action;
	}

	Device[] devices;
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return devices.length;
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return devices[position];
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(int position, View cv, final ViewGroup parent) {
		ViewHolder holder;
		final Device c = devices[position];
		
		if(cv == null){
			
			
			
			
			
			cv = inflator.inflate(R.layout.list_item_icon_text_arrow, null);
			TextView name = (TextView)cv.findViewById(R.id.text);
			ImageView icon = (ImageView)cv.findViewById(R.id.icon);
			ImageView arrow = (ImageView)cv.findViewById(R.id.arrow);
			 holder = new ViewHolder();

			 holder.title = name;
             holder.arrow = arrow;
             holder.icon = icon;
             holder.device = c;
             cv.setTag(holder);//��ViewHolder����  
             
             setAll(holder);
			//cv.setTag(c);
			
			
		}else{
			
			holder = (ViewHolder)cv.getTag();
			holder.device = c;
			setAll(holder);
			
	            
		}
		return cv;
	}
	void setAll(final ViewHolder holder){
		
		String dn = holder.device.getName();
		if(dn == null){
			
	         String dct = holder.device.getType();
	         
	         dn = IconManager.getString(context, dct);
		}
		
		holder.title.setText(dn);
		
		if(holder.device.getSubdevices().isEmpty())
			holder.arrow.setVisibility(View.GONE);
		else if(action){
			
			holder.arrow.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setPackage(context.getPackageName());
				intent.setClass(context, SubDevicesActivity.class);
				intent.putExtra("globalId", holder.device.getGlobalId());
				context.startActivity(intent);
				
			}
		});
		}
		
		
		String type = holder.device.getType();
		if(type == null || type.equals(""))
				type = "device";
		
		Drawable da;
		da = IconManager.getIconDrawable(context, type);
		holder.icon.setImageDrawable(da);
	}

	public final class ViewHolder{
	    public TextView title;
	    public ImageView arrow;
	    public ImageView icon;
	    public Device device;
	    }
}